import {Store,Module,ActionContext} from 'vuex'
import ListModule from '../list-module'
import ListState from '../list-state'
import GroupBuy from '@/store/entities/groupBuy/GroupBuy'
import Ajax from '../../../lib/ajax'
import PageResult from '@/store/entities/page-result';
import ListMutations from '../list-mutations'

interface GroupBuyState extends ListState<GroupBuy>{
    editGroupBuy: GroupBuy;
}

class GroupBuyModule extends ListModule<GroupBuyState,any,GroupBuy> {
    state={
        totalCount:0,
        currentPage:1,
        pageSize:10,
        list: new Array<GroupBuy>(),
        loading:false,
        editGroupBuy:new GroupBuy(),
    };
    actions = {
        async getAll(context:ActionContext<GroupBuyState,any>,payload:any){
            context.commit("setLoading",true);
            let reponse=await Ajax.get('/api/services/app/GroupBuy/GetAll',{params:payload.data});
            context.commit("setLoading",false);
            let page=reponse.data.result as PageResult<GroupBuy>;
            context.commit("setTotalCount",page.totalCount)
            context.commit("setList",page.items);
        },
        async create(context:ActionContext<GroupBuyState,any>,payload:any){
            await Ajax.post('/api/services/app/GroupBuy/Create',payload.data);
        },
        async update(context:ActionContext<GroupBuyState,any>,payload:any){
            await Ajax.put('/api/services/app/GroupBuy/Update',payload.data);
        },
        async delete(context:ActionContext<GroupBuyState,any>,payload:any){
            await Ajax.delete('/api/services/app/GroupBuy/Delete?Id='+payload.data.id);
        },
        async get(context:ActionContext<GroupBuyState,any>,payload:any){
            let reponse=await Ajax.get('/api/services/app/GroupBuy/Get?Id='+payload.id);
            return reponse.data.result as GroupBuy;
        }
    };
    mutations={
        setCurrentPage(state:GroupBuyState,page:number){
            state.currentPage=page;
        },
        setPageSize(state:GroupBuyState,pagesize:number){
            state.pageSize=pagesize;
        },
        setTotalCount(state:GroupBuyState,totalCount:number) {
            state.totalCount = totalCount;
        },
        setList(state:GroupBuyState,list: Array<GroupBuy>) {
            state.list = list;
        },
        setLoading(state:GroupBuyState,loading:boolean) {
            state.loading = loading;
        },
        edit(state:GroupBuyState,GroupBuy:GroupBuy){
            state.editGroupBuy=GroupBuy;
        }
    }
}

const groupBuyModule = new GroupBuyModule();
export default groupBuyModule;